#include "plate.h"
#include <cv.h>
#include <list>
#include <highgui.h>
#include "location.h"
#include "util.h"

char GetPlate(const char* filename, Region& region)
{
	IplImage* pRawImg; //声明IplImage指针
	IplImage* pResizeImg;
	IplImage* pFilterImg;
	IplImage* pEdgeImg;
	IplImage* pBinImg;

    //载入图像
	pRawImg = cvLoadImage(filename, 0);

	Resize(pRawImg, &pResizeImg, 512, 512);
	PreProcess(pResizeImg, &pFilterImg);
	Edge(pFilterImg, &pEdgeImg);
	Binary(pEdgeImg, &pBinImg);

	long* pHopMap = (long*)malloc((pBinImg->height * pBinImg->width) * sizeof(long));
	CreateHopMap(pBinImg, pHopMap);

	std::list<Box> blist;
	GetCandidatesFromHeight(pHopMap, pBinImg->height, pBinImg->width, blist);

	std::list<Rect> rlist;
	GetCandidatesFromWidth(pBinImg, blist, rlist);

	Rect rect;
	Select(rlist, pHopMap, rect);
	region.x1 = rect.x1-15;
	region.y1 = rect.y1;
	region.x2 = rect.x2;
	region.y2 = rect.y2;

	//cvRectangle(pBinImg,cvPoint(rect.x1, rect.y1), cvPoint(rect.x2, rect.y2), CV_RGB(255, 255, 255), 2, 8, 0);

	//char savename[100];
	//sprintf(savename, "finish/%s", filename);
	//printf("%s\n", savename);
	//cvSaveImage(savename, pBinImg);

//    cvNamedWindow( "Image", 1 );//创建窗口
//    cvShowImage( "Image", pResizeImg );//显示图像
//    cvWaitKey(0); //等待按键
//    cvDestroyWindow( "Image" );//销毁窗口

	free(pHopMap);
    cvReleaseImage( &pRawImg ); //释放图像
	cvReleaseImage( &pResizeImg );
	cvReleaseImage( &pFilterImg );
	cvReleaseImage( &pEdgeImg );
	cvReleaseImage( &pBinImg );

    return 0;
}

void ShowPlate(const char* filename, Region& region)
{
	IplImage* p;
	IplImage* p1;
	p = cvLoadImage(filename, 1);
	Resize(p, &p1, 512, 512);
	cvRectangle(p1,cvPoint(region.x1, region.y1), cvPoint(region.x2, region.y2), CV_RGB(255, 255, 255), 2, 8, 0);

	cvNamedWindow( filename, 1 );//创建窗口
    cvShowImage( filename, p1 );//显示图像

    cvWaitKey(0); //等待按键

    cvDestroyWindow( filename );//销毁窗口

	cvReleaseImage(&p);
	cvReleaseImage(&p1);
}

